using NFreeMarker.Template;
using NFreeMarker.Template.Utility;

namespace NFreeMarker.Core
{
    /**
     * A simple implementation of the <tt>TemplateNumberModel</tt>
     * interface. Note that this class is immutable.
     * @author <A HREF="mailto:jon@revusky.com">Jonathan Revusky</A>
     */
    internal sealed class NumberLiteral : Expression, ITemplateNumberModel
    {
        private readonly Number _value;

        public NumberLiteral(Number value)
        {
            _value = value;
        }

        protected override ITemplateModel _getAsTemplateModel(Environment env)
        {
            return new SimpleNumber(_value);
        }

        internal override string GetStringValue(Environment env)
        {
            return env.FormatNumber(_value);
        }

        public Number GetAsNumber()
        {
            return _value;
        }

        private string GetName()
        {
            return "the number: '" + _value + "'";
        }

        public override string GetCanonicalForm()
        {
            return _value.ToString();
        }

        internal override bool IsLiteral()
        {
            return true;
        }

        protected override Expression DeepCloneImpl(string name, Expression subst)
        {
            return new NumberLiteral(_value);
        }
    }
}